/**
 * Copyright (c) 2011-2021, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.generator.html;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.jfinal.generator.model.TableMeta;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;

/**
 * Html 生成器
 */
public class HtmlGenerator {

	protected Engine engine;
	protected String querytemplate = "/com/jfinal/generator/template/query_html_template.jf";
	protected String edittemplate = "/com/jfinal/generator/template/edit_html_template.jf";
	protected String htmlOutputDir;
	protected String bean;
	protected String path;
	
	public HtmlGenerator(String bean, String path, String htmlOutputDir) {
		this.bean = bean;
		this.path = path;
		this.htmlOutputDir = htmlOutputDir;
		initEngine();
	}

	protected void initEngine() {
		engine = new Engine();
		engine.setToClassPathSourceFactory();
		engine.addSharedMethod(new StrKit());
	}

	public void generate(TableMeta tableMeta) {
		System.out.println("Generate service ...");
		System.out.println("Html Output Dir: " + htmlOutputDir);
		try {
			writeToFileQuery(genQueryContent(tableMeta));
			writeToFileEdit(genEditContent(tableMeta));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	protected String genQueryContent(TableMeta tableMeta) {
		Kv data = Kv.by("bean", bean);
		data.set("path", path);
		data.set("tableMeta", tableMeta);

		return engine.getTemplate(querytemplate).renderToString(data);
	}

	protected String genEditContent(TableMeta tableMeta) {
		Kv data = Kv.by("bean", bean);
		data.set("path", path);
		data.set("tableMeta", tableMeta);

		return engine.getTemplate(edittemplate).renderToString(data);
	}

	/**
	 * 若文件存在，则不生成，以免覆盖用户手写的代码
	 */
	protected void writeToFileQuery(String Content) throws IOException {
		File dir = new File(htmlOutputDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}

		String target = htmlOutputDir + File.separator + "query.html";
		File file = new File(target);
		if (file.exists()) {
			return; // 若Service 存在，不覆盖
		}
		try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
			osw.write(Content);
		}
	}

	protected void writeToFileEdit(String Content) throws IOException {
		File dir = new File(htmlOutputDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}

		String target = htmlOutputDir + File.separator + "edit.html";
		File file = new File(target);
		if (file.exists()) {
			return; // 若Service 存在，不覆盖
		}
		try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
			osw.write(Content);
		}
	}
}
